package com.neusoft.neuiotms.dcvm.controller;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcvm.dto.ConfigDTO;
import com.neusoft.neuiotms.dcvm.service.ConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("配置管理")
@RestController
@RequestMapping(value = "/api/w/configs")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @ApiOperation("分页查询所有配置")
    @GetMapping("/p")
    public PageResultDTO<ConfigDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.configService.search(pageable, searchable));
    }

    @ApiOperation("配置详情")
    @GetMapping("/{id}")
    public ResultDTO<ConfigDTO> details(@PathVariable final String id) {
        ConfigDTO dto = null;
        if (NumberUtils.isCreatable(id)) {
            dto = this.configService.details(Long.valueOf(id));
        } else {
            dto = this.configService.details(id);
        }
        return ResultDTO.success(dto);
    }

    @ApiOperation("创建配置")
    @PostMapping
    public ResultDTO<ConfigDTO> create(@RequestBody final ConfigDTO dto) {
        final ConfigDTO result = this.configService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("修改配置")
    @PutMapping("/{id}")
    public ResultDTO<ConfigDTO> update(@PathVariable final Long id,
            @RequestBody final ConfigDTO dto) {
        final ConfigDTO result = this.configService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("删除配置")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.configService.delete(id);
        return ResultDTO.success();
    }

}
